package com.sl.mes.app.controller.mm;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.sl.mes.app.dao.mm.AggMoReqpickmVo;
import com.sl.mes.app.dao.mm.AggMoVo;
import com.sl.mes.app.service.mm.MmPickmBService;
import com.sl.mes.app.service.mm.MmReqpickmService;
import com.sl.mes.app.service.puboid.PubOidService;
import com.sl.mes.app.util.JsonHandler;
import com.sl.mes.core.util.JacksonUtil;
import com.sl.mes.core.util.ResponseUtil;
import com.sl.mes.db.service.mm.MmMoService;
import com.sl.mes.db.vo.*;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mes/mmreqpickm")
@Validated
public class MmReqpickmController {

   @Resource
    private MmReqpickmService mmReqpickmService;

    @Resource
    private PubOidService pubOidService;

    @Resource
    private MmMoService mmMoService;

    @Resource
    private MmPickmBService mmPickmBService;

    /**
     * 根据id查询配料单
     * @param body
     * @param request
     * @return
     */
    @PostMapping("getAggMoReqpickmVoById")
    public Object getAggMoVoById(@RequestBody String body, HttpServletRequest request) {
        String creqpickmid = JacksonUtil.parseString(body, "creqpickmid");
        Map<Object, Object> result = new HashMap<Object, Object>();
        AggMoReqpickmVo aggMoReqpickmVo = mmReqpickmService.getAggMoReqpickmVoById(creqpickmid);
        result.put("mmreqpickm",aggMoReqpickmVo);
        return ResponseUtil.ok(result);
    }

    /**
     * 根据id删除配料单
     * @param body
     * @param request
     * @return
     */
    @PostMapping("deleteAggMoReqpickmVoById")
    public Object deleteAggMoVoById(@RequestBody String body, HttpServletRequest request) {
        String creqpickmid = JacksonUtil.parseString(body, "creqpickmid");
        if(creqpickmid==null){
            return ResponseUtil.fail(-1,"您输入的id不存在");
        }
        Map<Object, Object> result = new HashMap<Object, Object>();
        AggMoReqpickmVo aggMoReqpickmVo = mmReqpickmService.deleteAggMoReqpickmVoById(creqpickmid);

        result.put("mmreqpickm",aggMoReqpickmVo);
        return ResponseUtil.ok(result);
    }

    /**
     * 添加配料单
     * @param body
     * @param request
     * @return
     */
    @PostMapping("addAggMoReqpickmVoById")
    public Object addAggMoVoById(@RequestBody String body, HttpServletRequest request) {
        JsonNode movo = (JsonNode) JacksonUtil.toNode(body);
        //组装AggMoReqpickmVo
        AggMoReqpickmVo aggMoReqpickmVo = new AggMoReqpickmVo();
        //表头vo
        JsonNode reqpickmHead = (JsonNode) ((JsonNode) ((JsonNode) movo.get("data")).get("mmreqpickm")).get("mmReqpickmHeadVo");
        MmReqpickm reqpickmHeadVo = JsonHandler.fromJson(reqpickmHead.toString(), MmReqpickm.class);
        String id = pubOidService.generate();
        reqpickmHeadVo.setCreqpickmid(id);
        aggMoReqpickmVo.setMmReqpickmHeadVo(reqpickmHeadVo);

        //表体vos
        ArrayNode moBody = (ArrayNode) ((JsonNode) ((JsonNode) movo.get("data")).get("mmreqpickm")).get("mmReqpickmBBodyVo");
        List<MmReqpickmB> lstReqpickmBBodyVo = new ArrayList<>();
        String[] ids = pubOidService.generate(moBody.size());
        for (int i = 0; i < moBody.size(); i++) {
            MmReqpickmB itemVo = JsonHandler.fromJson(moBody.get(i).toString(), MmReqpickmB.class);
            itemVo.setCreqpickmBid(ids[i]);
            itemVo.setCreqpickmid(id);
            lstReqpickmBBodyVo.add(itemVo);
        }
      aggMoReqpickmVo.setMmReqpickmBBodyVo(lstReqpickmBBodyVo);

        //调用service的insert方法
        int res = mmReqpickmService.addAggMoReqpickmVo(aggMoReqpickmVo);

        Map<Object, Object> result = new HashMap<Object, Object>();
        result.put("aggMoReqpickmVo", aggMoReqpickmVo);
        if (res>0){
            return ResponseUtil.ok(result);
        }else{
            return ResponseUtil.fail();
        }
    }

    /**
     * 修改备料申请
     *
     * @param body
     * @param request
     */
    @PostMapping("updataAggMoReqpickmVoById")
    public Object updataAggMoReqpickmVoById(@RequestBody String body, HttpServletRequest request) {
        JsonNode movo = (JsonNode) JacksonUtil.toNode(body);
        //表头vo
        AggMoReqpickmVo aggMoReqpickmVo = new AggMoReqpickmVo();

        JsonNode moReqpickmHead = (JsonNode) ((JsonNode) ((JsonNode) movo.get("data")).get("mmreqpickm")).get("mmReqpickmHeadVo");
        MmReqpickm mmReqpickmHeadVo = JsonHandler.fromJson(moReqpickmHead.toString(), MmReqpickm.class);
        aggMoReqpickmVo.setMmReqpickmHeadVo(mmReqpickmHeadVo);

        //表体vos
        ArrayNode moBody = (ArrayNode) ((JsonNode) ((JsonNode) movo.get("data")).get("mmreqpickm")).get("mmReqpickmBBodyVo");
        List<MmReqpickmB> list = new ArrayList<>();
        for (int i = 0; i < moBody.size(); i++) {
            MmReqpickmB itemVo = JsonHandler.fromJson(moBody.get(i).toString(), MmReqpickmB.class);
            list.add(itemVo);
            aggMoReqpickmVo.setMmReqpickmBBodyVo(list);
        }
        mmReqpickmService.updateAggMoReqpickmVo(aggMoReqpickmVo);
        return aggMoReqpickmVo;
    }

    /**
     * 收料工作
     * @param vrowno
     * @return
     */
    @GetMapping("getMmReqpickmByVrowno")
    public Object getMmReqpickmByVrowno(String vrowno){
        List<MmMo> lstmmMo = mmMoService.findChildrenVoByVrowno(vrowno);
        Map<Object, Object> result = new HashMap<Object, Object>();
        for (MmMo mmMo:lstmmMo){
            //流程订单号
            String vbillcode = mmMo.getVbillcode();
            result.put("vbillcode",vbillcode);

            //批次 数量
            List<MmReqpickmB>  lstmmReqpickmB =mmReqpickmService.selectMmReqpickmBByCmoid(mmMo.getCmoid());
            result.put("lstmmReqpickmB",lstmmReqpickmB);
        }
        return ResponseUtil.ok(result);
    }
}
